var o = getApp(), a = o.require("utils/util.js"), e = o.require("utils/api.js"), n = o.require("utils/enums.js"), t = o.require("utils/onfire.js"), l = 0, r = !1, i = !1, s = null, d = 0, c = {
    ErrandOrders: []
};

Page({
    data: {
        userInfo: null,
        dialogType: null,
        errandTypes: n.ErrandTypes,
        school_id: 0,
        school_name: null,
        lastPage: !1,
        inputNotice: "",
        categories: [],
        categoryIndex: 0,
        model: c
    },
    onLoad: function(a) {
        var n = this, l = o.getStorage("homeData");
        if (l.model) {
            var r = [];
            if (l.model.Keywords) for (var i in l.model.Keywords) {
                if (i % 8 == 0) {
                    var s = new Array(8);
                    r.push(s);
                }
                r[r.length - 1][i % 8] = l.model.Keywords[i];
            }
            n.setData({
                school_id: l.schoolId,
                school_name: l.schoolName,
                model: l.model,
                categories: r
            });
        }
        if (t.on("hideIndexDialog", function() {
            n.data.dialogType && n.setData({
                dialogType: null
            });
        }), t.on("refreshHomeOrders", function() {
            n.loadData();
        }), n.loadData(), o.globalData.schoolInfo) n.setData({
            school_id: o.globalData.schoolInfo.Id,
            school_name: o.globalData.schoolInfo.Name
        }); else {
            var d = function(a) {
                e.homeNearestSchool({
                    longitude: a.longitude,
                    latitude: a.latitude
                }, function(a) {
                    if (o.globalData.schoolInfo = a, n.setData({
                        school_id: a ? a.Id : 0,
                        school_name: a ? a.Name : "没有找到学校，重新定位"
                    }), a && a.Id) {
                        var e = o.getStorage("homeData");
                        e.schoolId = a.Id, e.schoolName = a.Name, e.save();
                    }
                });
            };
            o.globalData.locationInfo ? d(o.globalData.locationInfo) : t.on("location", function(o) {
                d(o);
            });
        }
    },
    onReady: function() {
        var o = this;
        t.on("login", function() {
            r = !0, d && (c.IsLogin || o.loadData());
        });
    },
    onShow: function() {
        var o = this;
        i || (i = !0, o.runNoticeAnimate()), t.un("SchoolSelect");
    },
    onHide: function() {
        s && (clearInterval(s), this.setData({
            inputNotice: "想让同学帮你干点什么..."
        }));
    },
    onUnload: function() {
        t.un("hideIndexDialog");
    },
    onPullDownRefresh: function() {
        l = 0, o.globalData.disableLoading = !0, this.loadData(null, function() {
            o.globalData.disableLoading = !1, wx.stopPullDownRefresh();
        });
    },
    onReachBottom: function() {
        this.data.lastPage || this.loadData({
            pageIndex: ++l
        });
    },
    onShareAppMessage: function() {},
    runNoticeAnimate: function() {
        var o = this, a = [ "I", "", "I", "", "I", "想", "想让", "想让同", "想让同学", "想让同学帮", "想让同学帮你", "想让同学帮你干", "想让同学帮你干点", "想让同学帮你干点什", "想让同学帮你干点什么", "想让同学帮你干点什么..." ], e = 0;
        s = setInterval(function() {
            o.setData({
                inputNotice: a[e]
            }), e == a.length - 1 ? clearInterval(s) : e++;
        }, 250);
    },
    loadData: function(n, t) {
        var l = this;
        e.homeIndex(n, function(e) {
            if (n && n.pageIndex) for (var i in e.ErrandOrders) c.ErrandOrders.push(e.ErrandOrders[i]); else {
                c = e;
                var s = function() {
                    var a = o.getStorage("homeData");
                    if (a.model && a.model.Banners && a.model.Banners.length) for (var n in a.model.Banners) {
                        var t = a.model.Banners[n].Src;
                        t.indexOf("http://store/") >= 0 && wx.removeSavedFile({
                            filePath: t,
                            complete: function(o) {
                                console.log(o && o.errMsg);
                            }
                        });
                    }
                    a.model = e, a.save();
                };
                e.Banners.length > 0 ? a.download(e.Banners[0].Src, function(o) {
                    e.Banners[0].Src = o, s();
                }) : s();
            }
            var u = [];
            if (c.Keywords) for (var i in c.Keywords) {
                if (i % 8 == 0) {
                    var g = new Array(8);
                    u.push(g);
                }
                u[u.length - 1][i % 8] = c.Keywords[i];
            }
            l.setData({
                userInfo: o.globalData.userInfo,
                lastPage: e.ErrandOrders.length < o.pageSize,
                model: c,
                categories: u
            }), r ? d < 2 && !c.IsLogin && (d = 2, l.loadData()) : d = 1, t && t();
        }, t);
    },
    onCategoryChange: function(o) {
        this.setData({
            categoryIndex: o.detail.current
        });
    },
    onSchoolTap: function() {
        var a = this;
        t.on("SchoolSelect", function(e) {
            o.globalData.schoolInfo = e, a.setData({
                school_id: e.Id,
                school_name: e.Name
            });
        }), wx.navigateTo({
            url: "../../mine/school/select"
        });
    },
    onDialogClose: function(o) {
        this.setData({
            dialogType: null
        });
    },
    onItemSubmitTap: function(o) {
        var a = o.currentTarget.dataset.index, e = c.ErrandOrders[a];
        wx.navigateTo({
            url: "info?id=" + e.Id + (2 != e.Status || e.IsForbiddenAccept ? "" : "&submit=true")
        });
    },
    onBannerTap: function(o) {
        var a = o.currentTarget.dataset.url;
        0 == a.indexOf("app://") ? wx.navigateTo({
            url: a.replace("app://", "/")
        }) : wx.navigateTo({
            url: "/pages/common/content/web?url=" + encodeURIComponent(a)
        });
    }
});